package com.daan.android;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;

public class TakePhoto extends Activity {
	private PhotoDbAdapter mDbHelper;
	
	protected ImageView iv = null;
	protected Bitmap b = null;
	
	private EditText lengthval;
	private EditText weightval;
	private EditText descriptionval;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.take_photo);
        Log.i(TakePhoto.class.getName(),"Activity Ready to Go!");
        
        mDbHelper = new PhotoDbAdapter(this);
        mDbHelper.open();

        final Button  btn = (Button) this.findViewById(R.id.TakePhoto);
        final Button  btnok = (Button) this.findViewById(R.id.ConfirmPhoto);
        lengthval = (EditText) this.findViewById(R.id.txtLength);
        weightval = (EditText) this.findViewById(R.id.txtWeight);
        descriptionval = (EditText) this.findViewById(R.id.txtDescription);
        
        btn.setOnClickListener(new View.OnClickListener(){

        	public void onClick(View v){
        		Log.i(TakePhoto.class.getName(),"Button Clicked!");
        		try {
        			Intent action = new Intent("android.media.action.IMAGE_CAPTURE");
        			action.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); 
        			startActivityForResult(action,1);
        		} catch (Exception e) {
        			Log.e(PhotoBooth.class.getName(),"Error occured [" + e.getMessage() + "]");
        		}
        	}
        });
        
        btnok.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            	float lengte = new Float(lengthval.getText().toString());
            	float gewicht = new Float(weightval.getText().toString());
            	String fragment = descriptionval.getText().toString();
            	Integer fotoindex = new Integer(mDbHelper.lastPhotoId() + 1);
            	String filename = "BT" + fotoindex.toString();
            	
            	if (b != null) {
    				
    				// Bewaar de fotogegevens in de database
    				
    				mDbHelper.createPhoto(lengte, gewicht, fragment);
	
    				// Save the name and description of an image in a ContentValues map.  
    		    	
    				android.provider.MediaStore.Images.Media.insertImage( getContentResolver(), b,  filename, fragment);
    			}
            	
                setResult(RESULT_OK);
                finish();
            }

        });

        iv = (ImageView) this.findViewById(R.id.PictureFrame);

    }
    
    protected void onActivityResult(int requestCode,int resultCode,Intent data)
    {
    	

    	try {
    		if (requestCode == 1) {
    			if (resultCode == RESULT_OK) {
	    			if (b != null) b.recycle();
	    			b = (Bitmap) data.getExtras().get("data");
	    			if (b != null) {
	    					    				
	    				iv.setImageBitmap(b);
	    			}
    			}
    		}
    	}catch (Exception e) {
    		Log.e(TakePhoto.class.getName(),"onActivityResult Error [" + e.getMessage() + "]");
    	}

    }
}